<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2016/4/19
 * Time: 17:42
 */
class security{
    public function index($id){
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $securityModel = M("module_security");
        Session::switchCustomer($customerId);
        $where['a.id']=array('eq',$id);
        $result = $securityModel->alias("a")
            ->join("wx_pb_template b on a.templateId=b.id")
            ->where($where)
            ->field("a.id,a.proImgs,a.json,a.proContent,a.proLogo,a.proTitle,a.followUrl,a.shareTitle,a.shareImg,a.shareDescrip,b.htmlname")
            ->find();
        if($result['proImgs']){
            $result['proImgs']=json_decode($result['proImgs'],true);
        }
        if($result['json']){
            $result['json']=json_decode($result['json'],true);
        }
        if(!$result['htmlname']){
            $result['htmlname']='security_three_column';
        }
        Output::instance()->module("security",$result['htmlname'])->object($result);
    }
    //验证
    public function verification($id,$code){
        $customerId = Session::$user['customerId'];
        $userName = Session::$user['userName'];
        Session::switchCustomer(0);
        $securityCode = M("module_security_code");
        Session::switchCustomer($customerId);
        $where['a.securityId']=array('eq',$id);
        $where['a.verificationCode']=array('eq',$code);
        $where['a.type']=array('neq',2);
        $securityInfo = $securityCode->alias('a')
            ->join('inner join wx_module_security b on a.securityId=b.id')
            ->where($where)
            ->field('a.type,a.verificationNum,a.proIndex,b.batch,b.securityInfo,b.amount')
            ->find();
        if($securityInfo){
            $data['verificationNum'] = $securityInfo['verificationNum']+1;
            if($securityInfo['type']==0){
                $data['type']=1;
                $data['validateTime'] = date("Y-m-d H:i:s");
                $data['userName']=$userName;
            }
            $proIndex=$securityInfo['proIndex'];
            if(!$securityInfo['proIndex']){
                $proIndex = $securityCode->where("securityId=$id and type=1")->count();
                $proIndex++;
                $data["proIndex"]=$proIndex;
            }
            $securityCode->alias('a')->where($where)->save($data);
            $securityInfo['securityInfo'] = str_replace('{{code}}',"<span>".$code."</span>",$securityInfo['securityInfo']);
            $securityInfo['securityInfo'] = str_replace('{{batch}}',"<span>".$securityInfo['batch']."</span>",$securityInfo['securityInfo']);
            $securityInfo['securityInfo'] = str_replace('{{proIndex}}',"<span>".$proIndex."</span>",$securityInfo['securityInfo']);
            $securityInfo['securityInfo'] = str_replace('{{verificationNum}}',"<span>".$securityInfo['verificationNum']."</span>",$securityInfo['securityInfo']);
            $result['type']=$securityInfo['type'];
            $result['securityInfo']=$securityInfo['securityInfo'];
            Output::instance()->object($result);
        }else{
            Output::instance()->resultCode('1',"防伪码不存在");
        }

    }
}